﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Windows.Forms;
namespace OnlineBusTicket
{
    public partial class TicketDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Component.OnlineBusTicketEntities db = new Component.OnlineBusTicketEntities();
                MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                var obj = db.Members.SingleOrDefault(x => x.UserName == u.UserName);
                var obj1 = db.BusticKetDetails.Where(x => x.MemberId == obj.MemberId).Select(x => x.BusTicketDetailID);
                var result = obj1.ToList();
                ddlBusticketID.DataSource = result;
                DataBind();
                int i = int.Parse(ddlBusticketID.Text);
                var obj2 = db.BusticKetDetails.SingleOrDefault(x => x.BusTicketDetailID == i);
                lblFullName.Text = obj2.FullName;
                lblAge.Text = obj2.Age;
                lblBusListID.Text = obj2.BusListId;
                lblBusCatelogy.Text = obj2.BusCatelogy;
                lblDeparturePlace.Text = obj2.DeparturePlace;
                lblArrivePlace.Text = obj2.ArrivePlace;
                lblStartDate.Text = Convert.ToDateTime(obj2.StartDate).ToShortDateString();
                lblTime.Text = Convert.ToString(obj2.Time);
                lblPrice.Text = Convert.ToInt32(obj2.Price).ToString();
                lblTicketStatus.Text = obj2.TicketStatus;
            }
        }

        protected void btnReturnTicket_Click(object sender, EventArgs e)
        {
            Component.OnlineBusTicketEntities db = new Component.OnlineBusTicketEntities();
            int i=int.Parse(ddlBusticketID.Text);
            var obj1=db.BusticKetDetails.SingleOrDefault(x=>x.BusTicketDetailID==i);
            if (obj1.TicketStatus == "activated")
            {
                obj1.TicketStatus = "deactivated";

                var obj2 = db.TotalSeats.SingleOrDefault(x => x.BusListId == lblBusListID.Text);
                obj2.Seat = obj2.Seat - 1;
                MessageBox.Show("This ticket was deactivated", "Infomation", MessageBoxButtons.OK);
                DateTime date1=DateTime.Today;
                if (obj1.StartDate==date1)
                {
                    obj1.Price =obj1.Price * 30 / 100;
                }
                else if (obj1.StartDate.AddDays(-1) == date1)
                {
                    obj1.Price = obj1.Price * 15 / 100;
                }
                else
                {
                    obj1.Price = 0;
                }
            }
            else
            {
                MessageBox.Show("You can't return ticket because this ticket is deactivated", "Warning", MessageBoxButtons.OK);
            }
            db.SaveChanges();
            Response.Redirect("~/Search.aspx");
        }

        protected void ddlBusticketID_TextChanged(object sender, EventArgs e)
        {
            Component.OnlineBusTicketEntities db = new Component.OnlineBusTicketEntities();
            int i = int.Parse(ddlBusticketID.Text);
            var obj2 = db.BusticKetDetails.SingleOrDefault(x => x.BusTicketDetailID == i);
            lblFullName.Text = obj2.FullName;
            lblAge.Text = obj2.Age;
            lblBusListID.Text = obj2.BusListId;
            lblBusCatelogy.Text = obj2.BusCatelogy;
            lblDeparturePlace.Text = obj2.DeparturePlace;
            lblArrivePlace.Text = obj2.ArrivePlace;
            lblStartDate.Text = Convert.ToDateTime(obj2.StartDate).ToShortDateString();
            lblTime.Text = Convert.ToString(obj2.Time);
            lblPrice.Text = Convert.ToInt32(obj2.Price).ToString();
            lblTicketStatus.Text = obj2.TicketStatus;
        }
    }
}